package history.controller;

import history.model.HistoryService;

import java.sql.Date;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import to.HistoryTO;
import to.MemberTO;

@Controller
public class HistoryController {
	@Autowired
	private HistoryService service;
	
	public HistoryController(HistoryService service){
		this.service = service;
	}
	
	//등록
	@RequestMapping("/ratingPage.do")
	public String ratingPage(){
		return "history/h_comment.tiles";
	}
	
	@RequestMapping("/insertHistory.do")
	public String insertHistory (HttpSession session, String rating, String ratingComment){
		HistoryTO hto = (HistoryTO) session.getAttribute("newHistory");
		hto.setRating(rating);
		hto.setRatingComment(ratingComment);
		try{
			service.insertHistory(hto);
			session.setAttribute("resultMessage", "이용해 주셔서 감사합니다.");
		}catch(SQLException e){
			e.printStackTrace();
			session.setAttribute("resultMessage", "SQL에러가 났습니다. 관리자에게 문의해 주세요.");
		}
		return "firstPage.tiles";
	}
	
	@RequestMapping("/checkRideHistory.do")
	public String checkRideHistory(){
		return "history/checkRideHistory.tiles";
	}
	/*
	@RequestMapping("/getRatingComment.do")
	public ModelAndView getRatingComment(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;	
		String no = request.getParameter("do");
		try {			
			HistoryTO  hto= service.getHistoryByNo(no);
			ArrayList<HistoryTO> list = service.getHistoryList();
			mv = new ModelAndView("history/history_view.tiles","hto",hto);
			service.getHistoryList();
			mv = new ModelAndView("history/h_list.tiles","list" ,list);
		} catch (SQLException e) {			
			e.printStackTrace();
			mv = new ModelAndView("/history/error.jsp","error_message",e.getMessage());
		}			
		return mv;
	}
	//수정
	
	public ModelAndView updateHistory (HistoryTO hto){
		ModelAndView mv = null;
		try{
			service.updateHistory(hto);
			mv = new ModelAndView("/history/h_comment.jsp");
		}catch(SQLException e){
			e.printStackTrace();
			mv = new ModelAndView("/history/error.jsp","error_message",e.getMessage());
		}
		return mv;
	}
	*/
	//삭제
	public ModelAndView deleteHistoryByNO (String no){
		ModelAndView mv = null;
		try{
			service.deleteHistoryByNO(no);
			mv = new ModelAndView("/history/h_comment.jsp");
		}catch(SQLException e){
			e.printStackTrace();
			mv = new ModelAndView("/history/error.jsp","error_message",e.getMessage());
		}
		return mv;
	}
	//전체 조회
	@RequestMapping("/getHistoryList.do")
	public ModelAndView getHistoryList (HttpSession session, String rideDate1, String rideDate2, Map<String, Object> map) throws SQLException{
		HashMap<String,Object> condition = new HashMap<String,Object>();
		condition.put("rideDate1", rideDate1);
		condition.put("rideDate2", rideDate2);
		MemberTO mto = (MemberTO) session.getAttribute("login_info");
		if(mto.getMemberType() == 0){
			condition.put("memberPhone", mto.getPhone());
		}else{
			condition.put("driverPhone", mto.getPhone());
		}
		map.put("result", service.getHistoryList(condition));
		return new ModelAndView("jsonview",map);
	}
}
/*
 * Calendar cal = Calendar.getInstance();
		String[] dateArr = rideDate1.split("-");
		cal.set(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]));
		condition.put("rideDate1", new Date(cal.getTimeInMillis()));
		dateArr = rideDate2.split("-");
		cal.set(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]));
		condition.put("rideDate2", new Date(cal.getTimeInMillis()));
*/